class RankComparer:
    """
    计算两个级别符号的相对级别高低
    """
    RANK_TYPE_EXTERNAL = "EXTERNAL"
    RANK_TYPE_MOODY = "MOODY"
    RANK_TYPE_FITCH = "FITCH"
    RANK_TYPE_SP = "SP"
    RANK_TYPE_QE = "QE"

    # 外部评级  包含国内的评级机构
    RANK_MAPPING_EXTERNAL = {
        "AAA": 0,
        "AA+": 1,
        "AA": 2,
        "AA-": 3,
        "A+": 4,
        "A": 5,
        "A-": 6,
        "BBB+": 7,
        "BBB": 8,
        "BBB-": 9,
        "BB+": 10,
        "BB": 11,
        "BB-": 12,
        "B+": 13,
        "B": 14,
        "B-": 15,
        "CCC": 16,
        "CC": 17,
        "C": 18
    }

    # 境外-穆迪
    RANK_MAPPING_MOODY = {
        "Aaa": 0,
        "Aa1": 1,
        "Aa2": 2,
        "Aa3": 3,
        "A1": 4,
        "A2": 5,
        "A3": 6,
        "Baa1": 7,
        "Baa2": 8,
        "Baa3": 9,
        "Ba1": 10,
        "Ba2": 11,
        "Ba3": 12,
        "B1": 13,
        "B2": 14,
        "B3": 15,
        "Caa1": 16,
        "Caa2": 17,
        "Caa3": 18,
        "Ca": 19,
        "C": 20

    }
    # 境外-惠誉
    RANK_MAPPING_FITCH = {
        "AAA": 0,
        "AA+": 1,
        "AA": 2,
        "AA-": 3,
        "A+": 4,
        "A": 5,
        "A-": 6,
        "BBB+": 7,
        "BBB": 8,
        "BBB-": 9,
        "BB+": 10,
        "BB": 11,
        "BB-": 12,
        "B+": 13,
        "B": 14,
        "B-": 15,
        "CCC+": 16,
        "CCC": 17,
        "CCC-": 18,
        "CC": 19,
        "C": 20
    }

    # 境外-标普
    RANK_MAPPING_SP = {
        "AAA": 0,
        "AA+": 1,
        "AA": 2,
        "AA-": 3,
        "A+": 4,
        "A": 5,
        "A-": 6,
        "BBB+": 7,
        "BBB": 8,
        "BBB-": 9,
        "BB+": 10,
        "BB": 11,
        "BB-": 12,
        "B+": 13,
        "B": 14,
        "B-": 15,
        "CCC+": 16,
        "CCC": 17,
        "CCC-": 18,
        "CC": 19,
        "C": 20
    }

    # QE
    RANK_MAPPING_QE = {
        "L1": 0,
        "L2+": 1,
        "L2": 2,
        "L2-": 3,
        "L3+": 4,
        "L3": 5,
        "L3-": 6,
        "L4+": 7,
        "L4": 8,
        "L4-": 9,
        "L5+": 10,
        "L5": 11,
        "L5-": 12,
        "L6+": 13,
        "L6": 14,
        "L6-": 15,
        "L7": 16,
        "L8": 17,
        "L9": 18
    }

    @staticmethod
    def compare(rank1: str, rank2: str, rankType: str = "QE") -> int:
        """
        比较两个级别的大小
        :param rank1: 级别符号1
        :param rank2: 级别符号2
        :param rankType: rank type,参考 RankData.py.RANK_TYPE_XXXX
        :return: 级别差值x， x>0: rank1低于rank2 x级， 0:相等， x<0:rank1高于rank2 x级
        """
        if rankType is None:
            # 默认按QE序列
            return RankComparer.RANK_MAPPING_QE
        if rankType == RankComparer.RANK_TYPE_EXTERNAL:
            rankMapping = RankComparer.RANK_MAPPING_EXTERNAL
        elif rankType == RankComparer.RANK_TYPE_MOODY:
            rankMapping = RankComparer.RANK_MAPPING_MOODY
        elif rankType == RankComparer.RANK_TYPE_FITCH:
            rankMapping = RankComparer.RANK_MAPPING_FITCH
        elif rankType == RankComparer.RANK_TYPE_SP:
            rankMapping = RankComparer.RANK_MAPPING_SP
        elif rankType == RankComparer.RANK_TYPE_QE:
            rankMapping = RankComparer.RANK_MAPPING_QE
        else:
            return 0

        rank1Idx = rankMapping[rank1] if rank1 in rankMapping.keys() else 999
        rank2Idx = rankMapping[rank2] if rank2 in rankMapping.keys() else 999

        return rank1Idx - rank2Idx
